/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-20
 * Time: 19:05
 */
public class AllSort {
    //插入排序
    public void Insert(int[] array){
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j;
            for (j = i - 1; j >= 0; j--) {
                if (temp < array[j]){
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j + 1] = temp;
        }
    }
    //希尔排序
    public void ShellSort(int[] array){
        int gap = array.length;
        while (gap > 1){
            gap /= 2;
            shell(array,gap);
        }
    }
    public void shell(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int temp = array[i];
            int j;
            for (j = i - gap; j >= 0; j -= gap) {
                if (temp < array[j]){
                    array[j + gap] = array[j];
                }else {
                    //break非常有必要
                    break;
                }
            }
            array[j + gap] = temp;
        }
    }
    //选择排序1
    public void Choice1(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[minIndex] > array[j]){
                    minIndex = j;
                }
            }
            if (i != minIndex) {
                swap(array, i, minIndex);
            }
        }
    }
    //选择排序2
    public void Choice2(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            int maxIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[minIndex] > array[j]){
                    minIndex = j;
                }
                if (array[maxIndex] < array[j]){
                    maxIndex = j;
                }
            }
            swap(array,i,minIndex);
            if (maxIndex == i){
                maxIndex = minIndex;
            }
            swap(array,array.length - 1,maxIndex);
        }
    }
    //交换
    public void swap(int[] array,int x,int y){
        int temp = array[x];
        array[x] = array[y];
        array[y] = temp;
    }
    //冒泡排序
    public void Bubble(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            int flg = -1;
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]){
                    swap(array,j,j + 1);
                    flg = 1;
                }
            }
            if (flg == -1){
                break;
            }
        }
    }
    //堆排序
    public void Heap(int[] array){
        createBigHeap(array);
        int end = array.length - 1;
        while (end > 0){
            swap(array,0,end);
            shiftDown(0,array,end);
            end--;
        }
    }
    //向下调整
    private void createBigHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            shiftDown(parent,array,array.length);
        }
    }
    //
    private void shiftDown(int parent, int[] array,int end) {
        int child = (parent * 2) + 1;
        //保证孩子节点不越界
        while (child < end){
            //找到孩子中的最大值
            if ((child + 1 < end) && array[child] < array[child + 1]){
                child = child + 1;
            }
            //孩子节点大于父节点
            if (array[child] > array[parent]){
                swap(array,child,parent);
                parent = child;
                child = (child * 2) + 1;
            }else {
                break;
            }
        }
    }
    //快速排序1
    public void quickSort1(int[] array){
        quick1(array,0,array.length - 1);
    }
    //
    private void quick1(int[] array, int start, int end) {
        if (start >= end) return;
        int pivot = partitionHoare(array,start,end);
        //递归左边
        quick1(array,start,pivot - 1);
        //递归右边
        quick1(array,pivot + 1,end);
    }
    //Hoare
    private int partitionHoare(int[] array,int left,int right){
        int key = array[left];
        int index = left;
        while (left < right){
            //1.取等号是避免无限死循环交换
            //2.先走right是因为right和left相遇的地方一定是比key小的
            while (left < right && array[right] >= key){
                right--;
            }
            while (left < right && array[left] <= key){
                left++;
            }
            swap(array,right,left);
        }
        swap(array,left,index);
        return left;
    }
    //////////////////////////////////////////////////////////////////////////
    //快速排序2
    public void quickSort2(int[] array){
        quick2(array,0,array.length - 1);
    }
    //
    private void quick2(int[] array, int start, int end) {
        if (start >= end) return;
        int pivot = partitionDig(array,start,end);
        //递归左边
        quick2(array,start,pivot - 1);
        //递归右边
        quick2(array,pivot + 1,end);
    }
    //挖坑法
    private int partitionDig(int[] array,int left,int right){
        int key = array[left];
        while (left < right){
            while (left < right && array[right] >= key){
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= key){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = key;
        return array[left];
    }
}
